package com.wtwd.device.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.dao.BloodPressureRecordMapper;
import com.wtwd.device.model.entity.BloodPressureInfo;
import com.wtwd.device.model.entity.BloodPressureRecord;
import com.wtwd.device.model.vo.RespEntity;
import com.wtwd.device.service.BloodPressureRecordService;
import com.wtwd.device.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author mjy
 * @date 2021/09/03
 * @description
 */
@Service
public class BloodPressureRecordServiceImpl implements BloodPressureRecordService {

    @Autowired
    private BloodPressureRecordMapper bloodPressureRecordMapper;


    @Override
    public RespEntity getDaysBloodPressureRecordList(Long deviceId, String startTime, String endTime) {
        List<BloodPressureRecord> bloodPressureRecords = bloodPressureRecordMapper.getDaysBloodPressureRecordList(deviceId, startTime, endTime);
        if (bloodPressureRecords != null && bloodPressureRecords.size() > 0) {
            bloodPressureRecords.stream().forEach(bloodPressureRecord -> {
                bloodPressureRecord.setBloodPressureInfoList(JSONArray.parseArray(bloodPressureRecord.getBloodPressureInfo(), BloodPressureInfo.class));
            });
        }
        return new RespEntity(RespCode.SUCCESS, bloodPressureRecords);
    }

    @Override
    public RespEntity getMonthBloodPressureRecordList(Long deviceId, String month) {
        List<BloodPressureRecord> bloodPressureRecords = bloodPressureRecordMapper.getMonthBloodPressureRecordList(deviceId, month);
        if (bloodPressureRecords != null && bloodPressureRecords.size() > 0) {
            bloodPressureRecords.stream().forEach(bloodPressureRecord -> {
                bloodPressureRecord.setBloodPressureInfoList(JSONArray.parseArray(bloodPressureRecord.getBloodPressureInfo(), BloodPressureInfo.class));
            });
        }
        return new RespEntity(RespCode.SUCCESS, bloodPressureRecords);
    }

    @Override
    public RespEntity addBloodPressureRecord(BloodPressureRecord bloodPressureRecord) {
        String time = DateUtils.format(bloodPressureRecord.getTime(), DateUtils.dateFormat5);
        BloodPressureRecord bloodPressureRecordOld = bloodPressureRecordMapper.getBloodPressureRecordByDeviceIdAndTime(bloodPressureRecord.getDeviceId(),time);
        if (bloodPressureRecordOld!=null){
            return new RespEntity(RespCode.TODAYS_DATA_ALREADY_EXIST);
        }
        bloodPressureRecord.setBloodPressureInfo(JSONArray.toJSONString(bloodPressureRecord.getBloodPressureInfoList()));
        bloodPressureRecord.setUploadTime(new Date().getTime());
        Integer rest = bloodPressureRecordMapper.addBloodPressureRecord(bloodPressureRecord);
        return new RespEntity(rest>0?RespCode.SUCCESS:RespCode.FAIL);
    }

    @Override
    public Integer clearBloodPressureRecordByDate(String date) {
        return bloodPressureRecordMapper.clearBloodPressureRecordByDate(date);
    }

    @Override
    public BloodPressureRecord getBloodPressureRecordByDeviceIdAndTime(Long id, String time) {
        return bloodPressureRecordMapper.getBloodPressureRecordByDeviceIdAndTime(id,time);
    }

    @Override
    public Integer deletePressureRecordByDeviceId(Long id) {
        return bloodPressureRecordMapper.deletePressureRecordByDeviceId(id);
    }
}
